﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using HF.Framework.DataClientDBAgent;
using HF.WorkFlow.DataContract;
using HF.Framework.SqlDataContract;
using HF.WorkFlow.Const;
namespace HF.WorkFlow.Template
{
    /// <summary>
    /// 流程代理 数据访问类Agency
    /// </summary>
    public class WorkAgency
    {
        #region  私有字段
        private SqlDataItem sqlDataItem = new SqlDataItem();
        private string[] fieldList = { "AgencyId", "AgencyFormUser", "AgencyToUser", "AgencyStartTime", "AgencyEndTime", "AgencyStatus", "WorkFlowId", "WorkTaskId", "Description" };
        private string tableName = "HF_Agency";
        private string keyField = "AgencyId";
        private string sqlString = "";

        #endregion

        #region  私有方法
        private void setParameter(WorkAgencyEntity entity)
        {
            sqlDataItem.ParameterList.Clear();
            sqlDataItem.AppendParameter("@AgencyId", entity.AgencyId, typeof(string));
            sqlDataItem.AppendParameter("@AgencyFormUser", entity.AgencyFormUser, typeof(string));
            sqlDataItem.AppendParameter("@AgencyToUser", entity.AgencyToUser, typeof(string));
            sqlDataItem.AppendParameter("@AgencyStartTime", entity.AgencyStartTime, typeof(string));
            sqlDataItem.AppendParameter("@AgencyEndTime", entity.AgencyEndTime, typeof(string));
            sqlDataItem.AppendParameter("@AgencyStatus", entity.AgencyStatus, typeof(char));
            sqlDataItem.AppendParameter("@WorkTaskId", entity.WorkTaskId, typeof(string));
            sqlDataItem.AppendParameter("@WorkFlowId", entity.WorkFlowId, typeof(string));
            sqlDataItem.AppendParameter("@Description", entity.Description, typeof(string));
        }

        private void setInsertSql()
        {
            string tmpValueList = "";
            string tmpFieldName = "";
            sqlString = "insert into " + tableName + "(";
            int tmpInt = this.fieldList.Length;
            for (int i = 0; i < tmpInt - 1; i++)
            {
                tmpFieldName = fieldList[i].ToString();
                sqlString = sqlString + tmpFieldName + ", ";
                tmpValueList = tmpValueList + "@" + tmpFieldName + ",";
            }
            tmpFieldName = this.fieldList[tmpInt - 1].ToString();
            sqlString = sqlString + tmpFieldName;
            tmpValueList = tmpValueList + "@" + tmpFieldName;
            this.sqlString = sqlString + ")values(" + tmpValueList + ")";
            sqlDataItem.CommandText = sqlString;
        }

        private void setUpdateSql()
        {
            string tmpFieldName = "";
            int tmpInt = this.fieldList.Length;
            sqlString = "update " + tableName + " set ";
            for (int i = 0; i < tmpInt - 1; i++)
            {
                tmpFieldName = fieldList[i].ToString();
                sqlString = sqlString + tmpFieldName + " = @" + tmpFieldName + ", ";
            }
            tmpFieldName = this.fieldList[tmpInt - 1].ToString();
            sqlString = sqlString + tmpFieldName + " = @" + tmpFieldName;
            sqlString = sqlString + " where " + keyField + "=@" + this.keyField;
            sqlDataItem.CommandText = sqlString;
        }


        #endregion

        #region  构造函数
        public WorkAgency()
        { }
        public static WorkAgency GetInstance()
        {
            return new WorkAgency();
        }
        #endregion

        #region  公有方法
        #region  基本方法
        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="entity">数据实体</param>
        /// <returns>影响的记录条数</returns>
        public int Insert(WorkAgencyEntity entity)
        {
            try
            {
                setInsertSql();//设定insert语句
                setParameter(entity);//设定参数
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteNonQuery(sqlDataItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity">数据实体</param>
        /// <returns>影响的记录条数</returns>
        public int Update(WorkAgencyEntity entity)
        {
            try
            {
                setUpdateSql();//设定insert语句
                setParameter(entity);//设定参数
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteNonQuery(sqlDataItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string agencyid)
        {
            string tmpSql = "select * from " + tableName + " where " + keyField + "=@" + keyField;
            SqlDataItem sqlItem = new SqlDataItem();
            sqlItem.CommandText = tmpSql;
            sqlItem.AppendParameter("@" + keyField, agencyid);
            ClientDBAgent agent = new ClientDBAgent();
            return agent.RecordExists(sqlItem);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="workTaskId">主键Id</param>
        /// <returns>影响的记录条数</returns>
        public void Delete(string agencyid)
        {
            try
            {
                string tmpSql = "delete from " + tableName + " where  " + keyField + "=@" + keyField;
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = tmpSql;
                sqlItem.AppendParameter("@" + keyField, agencyid);
                ClientDBAgent agent = new ClientDBAgent();
                agent.ExecuteNonQuery(sqlItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 获取一条数据记录
        /// </summary>
        /// <param name="WorkTaskId">主键Id</param>
        /// <returns>DataTable</returns>
        public DataTable GetWorkAgencyTable(string agencyid)
        {
            string tmpSql = "select * from " + tableName + " where " + keyField + "=@" + keyField;
            try
            {
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = tmpSql;
                sqlItem.AppendParameter("@" + keyField, agencyid);
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteDataTable(sqlItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        ///// <summary>
        ///// 获取一个数据实体
        ///// </summary>
        ///// <param name="workTaskId">主键Id</param>
        ///// <returns>DataTable</returns>
        //public WorkAgencyEntity GetWorkAgencyEntity(string agencyid)
        //{
        //    DataTable dt = GetWorkAgencyTable(agencyid);
        //    if (dt != null && dt.Rows.Count > 0)
        //    {
        //        return GetWorkAgencyEntity(dt.Rows[0]);
        //    }
        //    return null;
        //}

        #endregion

        #region  扩展方法
       
        /// <summary>
        /// 流程权限
        /// </summary>
        /// <param name="userid">用户</param>
        /// <returns></returns>
        public DataTable GetUserAgency(string userid)
        {
            try
            {
                string getSql = " select WorkFlowId,FlowCaption" +
                               " from HF_WorkFlow  " +
                               "where WorkFlowId in(select WorkFlowId from dbo.HF_Operator where OperContent=@userid)";
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = getSql;
                sqlItem.AppendParameter("@userid", userid);
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteDataTable(sqlItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 流程权限
        /// </summary>
        /// <param name="userid">角色、部门、岗位</param>
        /// <returns></returns>
        public DataTable GetDeptAgency(string userid)
        {
            try
            {
                string getSql = " select WorkFlowId,FlowCaption" +
                               " from HF_WorkFlow  " +
                               "where WorkFlowId in(select WorkFlowId from HF_Operator where OperDisplay in" +
                               "(select Caption from HF_Duty where DutyId in(select PostID from HF_User where UserId=@userid)))";
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = getSql;
                sqlItem.AppendParameter("@userid", userid);
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteDataTable(sqlItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion
        #endregion
    }
}

